home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15081 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.8 KB

  1. Path: uhura.phoenix.net!usenet
  2. From: Robert Sanford <roberts@lbms.com>
  3. Newsgroups: comp.lang.c++,comp.os.ms-windows.programmer.misc
  4. Subject: Re: Virtual function question
  5. Date: Tue, 02 Apr 1996 08:38:17 -0600
  6. Organization: LBMS, Inc
  7. Message-ID: <31613BD9.5B4D@lbms.com>
  8. References: <4jp6e9$ou5@dub-news-svc-1.compuserve.com>
  9. NNTP-Posting-Host: 205.241.99.122
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (Win95; I)
  14.  
  15. Ross Boylan wrote:
  16. > I am trying to define, a la Smalltalk, a mix-in which defines the
  17. > various comparison operators in terms of one fundamental operator, <.
  18. > typedef int bool;
  19. > class RBMagnitude {
  20. > public:
  21. > //subclass should redefine < with appropriate types
  22. >         virtual bool operator<(const RBMagnitude&) const= 0;
  23. >         virtual bool operator>(const RBMagnitude& x) const {return  *this <
  24. > x;};
  25. > // and many more
  26. > };
  27. > class A : public RBMagnitude {
  28. > public:
  29. >         virtual bool operator<(const A& a) const {return (this->n < a.n);};
  30. > private:
  31. >         int n;
  32. > };
  33. > class A test;
  34. > This fails in MSVC++ 4.0 with the error that A is virtual because the
  35. > operator<(const RBMagnitude&) is not defined.
  36. > It looks to me as if this is because the operator< for A has a
  37. > different type signature (it uses an A rather than an RBMagnitude).
  38. > If so, the problem is the language definition, not Microsoft's
  39. > implementation.
  40.  
  41. No, the problem is not with the language definition. The language does what
  42. it is supposed to. You have declared a different function because of the
  43. different signatures.
  44.  
  45. > 1) Is this interpretation correct?YES.
  46.  
  47. > 2) Is there any way around this problem?  Commenting out the
  48. > definition of RBMagnitude::operator< doesn't work, because
  49. > RBMagnitude::operator> requires it.  I suppose I could define a bogus
  50. > operator< which throws an exception, but this seems awkward (in
  51. > particular, if A and B are both RBMagnitudes and I ask A<B, I fail at
  52. > run time rather than compile time).
  53. > 1) My personal design perspective is that when you define a virtual function,
  54. it needs to be valid for all derived classes. You are comparing two
  55. instances of RBMagnitude with the virtual function definition declared in
  56. the base class. Why should the results be different for derived classes?
  57.  
  58. 2) A template function for operator < would work. There are example of this
  59. in the STL.
  60.  
  61. > I would appreciate e-mail; I'll summarize here.
  62.  
  63. oops ;)
  64.  
  65. -- 
  66. --------------------------------------
  67. /                                    /
  68. / Robert J. Sanford, Jr.             /
  69. / roberts@lbms.com                   /
  70. /                                    /
  71. / The opinions expressed by me do    /
  72. / not reflect those of my employer.  /
  73. /                                    /
  74. /                                    /
  75. -------------------------------------
  76.